//
//  AlertViewatuoHidden.m
//  u76ho
//
//  Created by m-air-01 on 16/1/14.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "AlertViewatuoHidden.h"
@interface AlertViewatuoHidden ()
{
    NSString * title;
    UIImageView * _imageView;
}
@end
@implementation AlertViewatuoHidden
-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)text
{
    self = [super initWithFrame:frame];
    if (self) {
        title = text;
        [self addScrenShot];
        [self addAlertView];
    }
    return self;
}
-(void)addScrenShot{
    _imageView = [[UIImageView alloc]initWithFrame:self.bounds];
    UIImage * image = [UIImage imageScreenView:RootWindow];
    _imageView.image = image;
    [self addSubview:_imageView];
}
-(void)addAlertView
{
    CGFloat width = [title widthOfSize:18]+10;
    if (width > (SCREEN_WIDTH - 60)) {
        width = (SCREEN_WIDTH - 60);
    }
    else
    {
        
    }
    CGFloat myHeight = [title heightOfSize:18 width:(SCREEN_WIDTH - 60)] + 20;
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-width)/2, SCREEN_HEIGHT/2-SIZETO6H(52)/2, width, myHeight)];
    view.alpha = 0.8;
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 5;
    view.backgroundColor = [UIColor blackColor];
    [self addSubview:view];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.width, view.height)];
    titleLab.text = title;
    titleLab.textColor = UIColorFromRGB(0x6ec844);
    titleLab.numberOfLines = 0;
    titleLab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleLab];
    
    [self animation:view addSize:YES];
}
-(void)removeFromSuperviewer
{
    _imageView.hidden = YES;
    [self animation:self addSize:NO];
    
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.3];
}
-(void)animation:(UIView *)view addSize:(BOOL)is
{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.3f;
    animation.repeatCount = 0;
    
    if (is) {
        animation.autoreverses = NO;
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        [view.layer addAnimation:animation forKey:@"alertyes"];
    }else{
        animation.autoreverses = YES;
        animation.fromValue = [NSNumber numberWithFloat:1.0f];
        animation.toValue = [NSNumber numberWithFloat:0.0f];
        [view.layer addAnimation:animation forKey:@"alertno"];
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
